import re


def split_string_into_list_of_triplets(stringSequence):
#regular expression to split the sequence into a list of codons
    split3 = re.compile(r".{1,3}", re.DOTALL).findall
    listSequence = split3(stringSequence)
    return listSequence

def get_AA_dict(stop = False):
    aaDict =  {\
        'phenylalanine': ['TTT', 'TTC'],\
        'leucine': ['CTT', 'CTC', 'CTA', 'CTG', 'TTA', 'TTG'], \
        'isoleucine': ['ATT', 'ATC', 'ATA'],\
        'methionine': ['ATG'],\
        'valine': ['GTT', 'GTC', 'GTA', 'GTG'],\
        'serine': ['TCT', 'TCC', 'TCA', 'TCG', 'AGT', 'AGC'],\
        'proline': ['CCT', 'CCC', 'CCA', 'CCG'],\
        'threonine': ['ACT', 'ACC', 'ACA', 'ACG'],\
        'alanine': ['GCT', 'GCC', 'GCA', 'GCG'],\
        'tyrosine': ['TAT', 'TAC'],\
        'histidine': ['CAT', 'CAC'],\
        'glutamine': ['CAA', 'CAG'],\
        'asparagine': ['AAT', 'AAC'],\
        'lysine': ['AAA', 'AAG'],\
        'aspartic_acid': ['GAT', 'GAC'],\
        'glutamic_acid': ['GAA', 'GAG'],\
        'cysteine': ['TGT', 'TGC'],\
        'arginine': ['CGT', 'CGC', 'CGA', 'CGG', 'AGA', 'AGG'],\
        'glycine': ['GGT', 'GGC', 'GGA', 'GGG'],\
        'tryptophan': ['TGG'], \
        }
    if stop:
        aaDict['stop'] = ['TAA', 'TAG', 'TGA']
    return aaDict

def get_codon_dict(noStop = False):
    codons = [\
        'AAA',\
        'AAC',\
        'AAG',\
        'AAT',\
        'ACA',\
        'ACC',\
        'ACG',\
        'ACT',\
        'AGA',\
        'AGC',\
        'AGG',\
        'AGT',\
        'ATA',\
        'ATC',\
        'ATG',\
        'ATT',\
        'CAA',\
        'CAC',\
        'CAG',\
        'CAT',\
        'CCA',\
        'CCC',\
        'CCG',\
        'CCT',\
        'CGA',\
        'CGC',\
        'CGG',\
        'CGT',\
        'CTA',\
        'CTC',\
        'CTG',\
        'CTT',\
        'GAA',\
        'GAC',\
        'GAG',\
        'GAT',\
        'GCA',\
        'GCC',\
        'GCG',\
        'GCT',\
        'GGA',\
        'GGC',\
        'GGG',\
        'GGT',\
        'GTA',\
        'GTC',\
        'GTG',\
        'GTT',\
        'TAA',\
        'TAC',\
        'TAG',\
        'TAT',\
        'TCA',\
        'TCC',\
        'TCG',\
        'TCT',\
        'TGA',\
        'TGC',\
        'TGG',\
        'TGT',\
        'TTA',\
        'TTC',\
        'TTG',\
        'TTT'\
        ]
    if noStop:
        codons.remove('TAA')
        codons.remove('TAG')
        codons.remove('TGA')
    codonDict = {}
    for codon in codons:
        codonDict[codon] = 0
    return codonDict

def get_codon_to_AA_dict(noStop = False):
    aaDict = get_AA_dict()
    codons = [\
        'AAA',\
        'AAC',\
        'AAG',\
        'AAT',\
        'ACA',\
        'ACC',\
        'ACG',\
        'ACT',\
        'AGA',\
        'AGC',\
        'AGG',\
        'AGT',\
        'ATA',\
        'ATC',\
        'ATG',\
        'ATT',\
        'CAA',\
        'CAC',\
        'CAG',\
        'CAT',\
        'CCA',\
        'CCC',\
        'CCG',\
        'CCT',\
        'CGA',\
        'CGC',\
        'CGG',\
        'CGT',\
        'CTA',\
        'CTC',\
        'CTG',\
        'CTT',\
        'GAA',\
        'GAC',\
        'GAG',\
        'GAT',\
        'GCA',\
        'GCC',\
        'GCG',\
        'GCT',\
        'GGA',\
        'GGC',\
        'GGG',\
        'GGT',\
        'GTA',\
        'GTC',\
        'GTG',\
        'GTT',\
        'TAA',\
        'TAC',\
        'TAG',\
        'TAT',\
        'TCA',\
        'TCC',\
        'TCG',\
        'TCT',\
        'TGA',\
        'TGC',\
        'TGG',\
        'TGT',\
        'TTA',\
        'TTC',\
        'TTG',\
        'TTT'\
        ]
    if noStop:
        codons.remove('TAA')
        codons.remove('TAG')
        codons.remove('TGA')
    codonDict = {}
    for codon in codons:
        for aa in aaDict:
            if codon in aaDict[aa]:
                codonDict[codon] = aa
    return codonDict

